package com.walming.generator.copycat.utils;

import com.walming.generator.copycat.convert.CamelConvert;
import com.walming.generator.copycat.enums.ColumnNamingStrategy;
import com.walming.generator.copycat.exception.CopycatException;

/**
 * copycat工具类
 *
 * @author walming
 * @date 2020-06-03 15:17
 */
public final class CriteriaUtils {

    private CriteriaUtils() {
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * 根据不同的命名策略构造不同的属性字段名
     *
     * @param field    原始字段
     * @param strategy 数据库命名策略
     * @return 用于 SQL 查询的字段名
     */
    public static String columnName(String field, ColumnNamingStrategy strategy) {
        switch (strategy) {
            case FIRST_CAPITAL_CAMEL:
                return CriteriaUtils.toCapitalCamel(field);
            case FIRST_LOWER_CASE_CAMEL:
                return CriteriaUtils.toLowerCaseCamel(field);
            case LOWER_CASE_UNDER_LINE:
                return CriteriaUtils.toUnderscore(field);
            case UPPER_CASE_UNDER_LINE:
                return CriteriaUtils.toUnderscore(field).toUpperCase();
            default:
                throw new CopycatException("unknown column naming strategy:%s", strategy.name());
        }
    }

    /** 驼峰转下划线 */
    public static String toUnderscore(final String field) {
        return CamelConvert.toUnderscore(field);
    }

    /** 下划线转驼峰（首字母大写） */
    public static String toCapitalCamel(final String field) {
        return CamelConvert.toCamel(field, true);
    }

    /** 下划线转驼峰（首字母小写） */
    public static String toLowerCaseCamel(final String field) {
        return CamelConvert.toCamel(field, false);
    }

}
